package com.sky.service.impl;

import com.sky.mapper.ReportMapper;
import com.sky.mapper.WorkspaceMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private WorkspaceMapper workspaceMapper;

    /**
     * 获取今日运营数据
     * @return
     */
    public BusinessDataVO getBusinessData() {
        LocalDate date = LocalDateTime.now().toLocalDate();

        Integer newUserCount = workspaceMapper.getNewUserCount(date);
        newUserCount = newUserCount == null ? 0 : newUserCount;
        Integer totalOrder = workspaceMapper.getTotalOrder(date);
        totalOrder = totalOrder == null ? 0 : totalOrder;
        Integer totalCompletedOrder = workspaceMapper.getTotalCompletedOrder(date);
        totalCompletedOrder = totalCompletedOrder == null ? 0 : totalCompletedOrder;
        Double turnover = workspaceMapper.getTurnover(date);
        turnover = turnover == null ? 0 : turnover;

        return BusinessDataVO.builder().newUsers(newUserCount)
                .validOrderCount(totalCompletedOrder)
                .orderCompletionRate(totalOrder == 0 ? 0 : totalCompletedOrder.doubleValue() / totalOrder.doubleValue())
                .turnover(turnover)
                .unitPrice(totalCompletedOrder==0 ? 0 : turnover / totalCompletedOrder)
                .build();
    }


    /**
     * 获取套餐总览
     * @return
     */
    public SetmealOverViewVO overviewSetmeals() {
        Integer discontinued = workspaceMapper.getSetmealDiscontinued();
        discontinued = discontinued == null ? 0 : discontinued;
        Integer sold = workspaceMapper.getSetmealSold();
        sold = sold == null ? 0 : sold;

        return SetmealOverViewVO.builder().sold(sold).discontinued(discontinued).build();
    }

    /**
     * 获取菜品总览
     * @return
     */
    public DishOverViewVO overviewDishes() {
        Integer discontinued = workspaceMapper.getDishDiscontinued();
        discontinued = discontinued == null ? 0 : discontinued;
        Integer sold = workspaceMapper.getDishSold();
        sold = sold == null ? 0 : sold;

        return DishOverViewVO.builder().sold(sold).discontinued(discontinued).build();
    }

    /**
     * 获取订单总览
     * @return
     */
    public OrderOverViewVO overviewOrders() {
        Integer waitingOrders = workspaceMapper.getWaitingOrders();
        waitingOrders = waitingOrders == null ? 0 : waitingOrders;
        Integer deliveredOrders = workspaceMapper.getDeliveredOrders();
        deliveredOrders = deliveredOrders == null ? 0 : deliveredOrders;
        Integer completedOrders = workspaceMapper.getCompletedOrders();
        completedOrders = completedOrders == null ? 0 : completedOrders;
        Integer cancelledOrders = workspaceMapper.getCancelledOrders();
        cancelledOrders = cancelledOrders == null ? 0 : cancelledOrders;
        Integer allOrders = workspaceMapper.getAllOrders();
        allOrders = allOrders == null ? 0 : allOrders;

        return OrderOverViewVO.builder().allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .waitingOrders(waitingOrders).build();
    }
}
